<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/xhtml;charset=UTF-8"/>
<meta http-equiv="X-UA-Compatible" content="IE=9"/>
<title>Freetype OpenGL: demo-cartoon.c</title>

<link href="tabs.css" rel="stylesheet" type="text/css"/>
<link href="style.css" rel="stylesheet" type="text/css" />



</head>
<body>
<div id="content"><!-- do not remove this div! -->

<div id="top"><!-- do not remove this div! -->

  
  <div id="titlearea">
    
    <div id="projectname">Freetype OpenGL</div>
    
    
    <div id="projectbrief">A high quality OpenGL text rendering engine for C</div>
    
  </div>
  

<!--

<div id="titlearea">
<table cellspacing="0" cellpadding="0">
 <tbody>
 <tr style="height: 56px;">
  
  
  <td style="padding-left: 0.5em;">
   <div id="projectname">Freetype OpenGL
   &#160;<span id="projectnumber">0.1</span>
   </div>
   <div id="projectbrief">A high quality OpenGL text rendering engine for C</div>
  </td>
  
  
  
   
  
 </tr>
 </tbody>
</table>
</div>
-->


<!-- Generated by Doxygen 1.8.0 -->
<script type="text/javascript" src="dynsections.js"></script>
</div>
<div class="header">
  <div class="headertitle">
<div class="title">demo-cartoon.c</div>  </div>
</div><!--header-->
<div class="contents">
<div class="fragment"><pre class="fragment"><span class="comment">/* =========================================================================</span>
<span class="comment"> * Freetype GL - A C OpenGL Freetype engine</span>
<span class="comment"> * Platform:    Any</span>
<span class="comment"> * WWW:         http://code.google.com/p/freetype-gl/</span>
<span class="comment"> * -------------------------------------------------------------------------</span>
<span class="comment"> * Copyright 2011,2012 Nicolas P. Rougier. All rights reserved.</span>
<span class="comment"> *</span>
<span class="comment"> * Redistribution and use in source and binary forms, with or without</span>
<span class="comment"> * modification, are permitted provided that the following conditions are met:</span>
<span class="comment"> *</span>
<span class="comment"> *  1. Redistributions of source code must retain the above copyright notice,</span>
<span class="comment"> *     this list of conditions and the following disclaimer.</span>
<span class="comment"> *</span>
<span class="comment"> *  2. Redistributions in binary form must reproduce the above copyright</span>
<span class="comment"> *     notice, this list of conditions and the following disclaimer in the</span>
<span class="comment"> *     documentation and/or other materials provided with the distribution.</span>
<span class="comment"> *</span>
<span class="comment"> * THIS SOFTWARE IS PROVIDED BY NICOLAS P. ROUGIER &#39;&#39;AS IS&#39;&#39; AND ANY EXPRESS OR</span>
<span class="comment"> * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF</span>
<span class="comment"> * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO</span>
<span class="comment"> * EVENT SHALL NICOLAS P. ROUGIER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT,</span>
<span class="comment"> * INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES</span>
<span class="comment"> * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;</span>
<span class="comment"> * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND</span>
<span class="comment"> * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT</span>
<span class="comment"> * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF</span>
<span class="comment"> * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.</span>
<span class="comment"> *</span>
<span class="comment"> * The views and conclusions contained in the software and documentation are</span>
<span class="comment"> * those of the authors and should not be interpreted as representing official</span>
<span class="comment"> * policies, either expressed or implied, of Nicolas P. Rougier.</span>
<span class="comment"> * ========================================================================= */</span>
<span class="preprocessor">#include &quot;freetype-gl.h&quot;</span>
<span class="preprocessor">#include &quot;<a class="code" href="vertex-buffer_8h.html">vertex-buffer.h</a>&quot;</span>
<span class="preprocessor">#include &quot;<a class="code" href="markup_8h.html">markup.h</a>&quot;</span>


<span class="comment">// ------------------------------------------------------- typedef &amp; struct ---</span>
<span class="keyword">typedef</span> <span class="keyword">struct </span>{
    <span class="keywordtype">float</span> x, y, z;
    <span class="keywordtype">float</span> u, v;
    <a name="_a0"></a><a class="code" href="unionvec4.html" title="Tuple of 4 floats.">vec4</a> color;
} vertex_t;


<span class="comment">// ------------------------------------------------------- global variables ---</span>
<a name="_a1"></a><a class="code" href="structtexture__atlas__t.html" title="A texture atlas is used to pack several small regions into a single texture.">texture_atlas_t</a> * atlas;
<a name="_a2"></a><a class="code" href="structvertex__buffer__t.html" title="Generic vertex buffer.">vertex_buffer_t</a> * buffer;


<span class="comment">// ---------------------------------------------------------------- display ---</span>
<span class="keywordtype">void</span> display( <span class="keywordtype">void</span> )
{
    glClearColor( 0.40, 0.40, 0.45, 1.00 );
    glClearColor( 1.0, 1.0, 1.0, 1.0 );
    glClear( GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT );
    glEnable( GL_BLEND );
    glEnable( GL_TEXTURE_2D );
    glEnable( GL_COLOR_MATERIAL );
    glTexEnvi( GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_MODULATE );
    glBlendFunc( GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA );
    <a name="a3"></a><a class="code" href="group__vertex-buffer.html#ga48df5bafd9315aabfb7f14bedb778c16" title="Render vertex buffer.">vertex_buffer_render</a>( buffer, GL_TRIANGLES, <span class="stringliteral">&quot;vtc&quot;</span> );
    glutSwapBuffers( );
}


<span class="comment">// ---------------------------------------------------------------- reshape ---</span>
<span class="keywordtype">void</span> reshape(<span class="keywordtype">int</span> width, <span class="keywordtype">int</span> height)
{
    glViewport(0, 0, width, height);
    glMatrixMode(GL_PROJECTION);
    glLoadIdentity();
    glOrtho(0, width, 0, height, -1, 1);
    glMatrixMode(GL_MODELVIEW);
    glutPostRedisplay();
}

<span class="comment">// --------------------------------------------------------------- keyboard ---</span>
<span class="keywordtype">void</span> keyboard( <span class="keywordtype">unsigned</span> <span class="keywordtype">char</span> key, <span class="keywordtype">int</span> x, <span class="keywordtype">int</span> y )
{
    <span class="keywordflow">if</span> ( key == 27 )
    {
        exit( EXIT_SUCCESS );
    }
}


<span class="comment">// --------------------------------------------------------------- add_text ---</span>
<span class="keywordtype">void</span> add_text( <a class="code" href="structvertex__buffer__t.html" title="Generic vertex buffer.">vertex_buffer_t</a> * buffer, <a name="_a4"></a><a class="code" href="structtexture__font__t.html" title="Texture font structure.">texture_font_t</a> * font,
               <span class="keywordtype">wchar_t</span> *text, <a name="_a5"></a><a class="code" href="unionvec2.html" title="Tuple of 2 floats.">vec2</a> pen, <a class="code" href="unionvec4.html" title="Tuple of 4 floats.">vec4</a> fg_color_1, <a class="code" href="unionvec4.html" title="Tuple of 4 floats.">vec4</a> fg_color_2 )
{
    <span class="keywordtype">size_t</span> i;
    <span class="keywordflow">for</span>( i=0; i&lt;wcslen(text); ++i )
    {
        <a name="_a6"></a><a class="code" href="structtexture__glyph__t.html" title="A structure that describe a glyph.">texture_glyph_t</a> *glyph = <a name="a7"></a><a class="code" href="group__texture-font.html#ga06e988448ef6e118aa6e273df5892937" title="Request a new glyph from the font.">texture_font_get_glyph</a>( font, text[i] );
        <span class="keywordtype">float</span> kerning = 0;
        <span class="keywordflow">if</span>( i &gt; 0)
        {
            kerning = <a name="a8"></a><a class="code" href="group__texture-font.html#ga77d1adb569e7eaa4aaf776dc28415c66" title="Get the kerning between two horizontal glyphs.">texture_glyph_get_kerning</a>( glyph, text[i-1] );
        }
        pen.<a name="a9"></a><a class="code" href="unionvec2.html#a002d3519d48fe3cd79729b5b0ded74bf" title="Alias for first component.">x</a> += kerning;

        <span class="comment">/* Actual glyph */</span>
        <span class="keywordtype">float</span> x0  = ( pen.<a class="code" href="unionvec2.html#a002d3519d48fe3cd79729b5b0ded74bf" title="Alias for first component.">x</a> + glyph-&gt;<a name="a10"></a><a class="code" href="structtexture__glyph__t.html#a1f267cdba6bd1aff244ff72792ab1a38" title="Glyph&#39;s left bearing expressed in integer pixels.">offset_x</a> );
        <span class="keywordtype">float</span> y0  = (int)( pen.<a name="a11"></a><a class="code" href="unionvec2.html#a6d28b12b511da692550fc9d37b4e9b1d" title="Alias for second component.">y</a> + glyph-&gt;<a name="a12"></a><a class="code" href="structtexture__glyph__t.html#aeacd07f592dfa953f26b2e5c1863a814" title="Glyphs&#39;s top bearing expressed in integer pixels.">offset_y</a> );
        <span class="keywordtype">float</span> x1  = ( x0 + glyph-&gt;<a name="a13"></a><a class="code" href="structtexture__glyph__t.html#a51379921bd46b454fc56b7d28381af03" title="Glyph&#39;s width in pixels.">width</a> );
        <span class="keywordtype">float</span> y1  = (int)( y0 - glyph-&gt;<a name="a14"></a><a class="code" href="structtexture__glyph__t.html#a7a0e2fc48006cf6815e5b1e7dae8d386" title="Glyph&#39;s height in pixels.">height</a> );
        <span class="keywordtype">float</span> s0 = glyph-&gt;<a name="a15"></a><a class="code" href="structtexture__glyph__t.html#adcfb6e5a1b583f96c96d87b006667f2b" title="First normalized texture coordinate (x) of top-left corner.">s0</a>;
        <span class="keywordtype">float</span> t0 = glyph-&gt;<a name="a16"></a><a class="code" href="structtexture__glyph__t.html#ab2fec7dd584f5cfaf14c3b8a807d5e70" title="Second normalized texture coordinate (y) of top-left corner.">t0</a>;
        <span class="keywordtype">float</span> s1 = glyph-&gt;<a name="a17"></a><a class="code" href="structtexture__glyph__t.html#a3849b83a8bb51a6fc32ce77b485d9e95" title="First normalized texture coordinate (x) of bottom-right corner.">s1</a>;
        <span class="keywordtype">float</span> t1 = glyph-&gt;<a name="a18"></a><a class="code" href="structtexture__glyph__t.html#aee3ba35a29e1646df9cfaca6c0db3488" title="Second normalized texture coordinate (y) of bottom-right corner.">t1</a>;
        GLuint index = buffer-&gt;<a name="a19"></a><a class="code" href="structvertex__buffer__t.html#a54efd077fb2990b3679d060baaa021ec" title="Vector of vertices.">vertices</a>-&gt;<a name="a20"></a><a class="code" href="structvector__t.html#a7ec97862387465722f8f08f8f8f69371" title="Number of items.">size</a>;
        GLuint indices[] = {index, index+1, index+2,
                            index, index+2, index+3};
        vertex_t vertices[] = {
            { (int)x0,y0,0,  s0,t0,  fg_color_1 },
            { (int)x0,y1,0,  s0,t1,  fg_color_2 },
            { (int)x1,y1,0,  s1,t1,  fg_color_2 },
            { (int)x1,y0,0,  s1,t0,  fg_color_1 } };
        vertex_buffer_push_back_indices( buffer, indices, 6 );
        vertex_buffer_push_back_vertices( buffer, vertices, 4 );
        pen.<a class="code" href="unionvec2.html#a002d3519d48fe3cd79729b5b0ded74bf" title="Alias for first component.">x</a> += glyph-&gt;<a name="a21"></a><a class="code" href="structtexture__glyph__t.html#aba3ece1130e9d96562a5c2ec3e60da4c" title="For horizontal text layouts, this is the horizontal distance (in fractional pixels) used to increment...">advance_x</a>;
    }
}

<span class="comment">// ------------------------------------------------------------------- main ---</span>
<span class="keywordtype">int</span> main( <span class="keywordtype">int</span> argc, <span class="keywordtype">char</span> **argv )
{
    <span class="keywordtype">size_t</span> width = 600, height = 200;

    glutInit( &amp;argc, argv );
    glutInitWindowSize( width, height );
    glutInitDisplayMode( GLUT_DOUBLE | GLUT_RGB | GLUT_DEPTH );
    glutCreateWindow( <span class="stringliteral">&quot;Glyph Cartoon&quot;</span> );
    glutReshapeFunc( reshape );
    glutDisplayFunc( display );
    glutKeyboardFunc( keyboard );

    atlas = <a name="a22"></a><a class="code" href="group__texture-atlas.html#ga7769be70f630e0a8a8b320525a07382d" title="Creates a new empty texture atlas.">texture_atlas_new</a>( 1024, 1024, 1 );
    buffer = <a name="a23"></a><a class="code" href="group__vertex-buffer.html#ga309c1129a1028f251fe6e6c3c1534a42" title="Creates an empty vertex buffer.">vertex_buffer_new</a>( <span class="stringliteral">&quot;v3f:t2f:c4f&quot;</span> ); 
    <a class="code" href="structtexture__font__t.html" title="Texture font structure.">texture_font_t</a> *font = <a name="a24"></a><a class="code" href="group__texture-font.html#ga904424e7b0e2be14b694599b5c483e64" title="This function creates a new texture font from given filename and size.">texture_font_new</a>( atlas, <span class="stringliteral">&quot;fonts/ObelixPro.ttf&quot;</span>, 128 );

    <a class="code" href="unionvec2.html" title="Tuple of 2 floats.">vec2</a> pen    = {{30, 50}};
    <a class="code" href="unionvec4.html" title="Tuple of 4 floats.">vec4</a> black  = {{0.0, 0.0, 0.0, 1.0}};
    <a class="code" href="unionvec4.html" title="Tuple of 4 floats.">vec4</a> yellow = {{1.0, 1.0, 0.0, 1.0}};
    <a class="code" href="unionvec4.html" title="Tuple of 4 floats.">vec4</a> orange1 = {{1.0, 0.9, 0.0, 1.0}};
    <a class="code" href="unionvec4.html" title="Tuple of 4 floats.">vec4</a> orange2 = {{1.0, 0.6, 0.0, 1.0}};


    font-&gt;<a name="a25"></a><a class="code" href="structtexture__font__t.html#afeb9b856800fae9d5f01328022566f08" title="Outline type (0 = None, 1 = line, 2 = inner, 3 = outer)">outline_type</a> = 2;
    font-&gt;<a name="a26"></a><a class="code" href="structtexture__font__t.html#a2ca8ecdb553c2fcf6226b6c65f157902" title="Outline thickness.">outline_thickness</a> = 7;
    add_text( buffer, font, L<span class="stringliteral">&quot;Cartoon&quot;</span>, pen, black, black );

    font-&gt;<a class="code" href="structtexture__font__t.html#afeb9b856800fae9d5f01328022566f08" title="Outline type (0 = None, 1 = line, 2 = inner, 3 = outer)">outline_type</a> = 2;
    font-&gt;<a class="code" href="structtexture__font__t.html#a2ca8ecdb553c2fcf6226b6c65f157902" title="Outline thickness.">outline_thickness</a> = 5;
    add_text( buffer, font, L<span class="stringliteral">&quot;Cartoon&quot;</span>, pen, yellow, yellow );

    font-&gt;<a class="code" href="structtexture__font__t.html#afeb9b856800fae9d5f01328022566f08" title="Outline type (0 = None, 1 = line, 2 = inner, 3 = outer)">outline_type</a> = 1;
    font-&gt;<a class="code" href="structtexture__font__t.html#a2ca8ecdb553c2fcf6226b6c65f157902" title="Outline thickness.">outline_thickness</a> = 3;
    add_text( buffer, font, L<span class="stringliteral">&quot;Cartoon&quot;</span>, pen, black, black );

    font-&gt;<a class="code" href="structtexture__font__t.html#afeb9b856800fae9d5f01328022566f08" title="Outline type (0 = None, 1 = line, 2 = inner, 3 = outer)">outline_type</a> = 0;
    font-&gt;<a class="code" href="structtexture__font__t.html#a2ca8ecdb553c2fcf6226b6c65f157902" title="Outline thickness.">outline_thickness</a> = 0;
    add_text( buffer, font, L<span class="stringliteral">&quot;Cartoon&quot;</span>, pen, orange1, orange2 );

    glutMainLoop( );
    <span class="keywordflow">return</span> 0;
}
</pre></div> </div><!-- contents -->
</div>
</body>
</html>
